package shuaige.jdk.stream.collectors相关的方法;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class CollectingAndThen归纳结果再处理 {
    /**
     * 该方法是在归纳动作结束之后，对归纳的结果进行再处理。
     */
    public static void main(String[] args) {
        List<String> list = Arrays.asList("111", "222", "333", "444", "555", "666", "777", "888");
        int length = list.stream().collect(Collectors.collectingAndThen(Collectors.toList(), strings -> {
            int size = strings.size();
            return size;
        }));
//        System.out.println(length);
        创建一个不变的Map();
    }

    public static void 创建不可变列表() {
        // Create an Immutable List
        List<String> lt = Stream
                .of("111", "222", "333")
                .collect(Collectors.collectingAndThen(Collectors.toList(), Collections::<String>unmodifiableList));
        System.out.println(lt);
    }


    public static void 创建一个不变的Map() {
        // Create an Immutable Map
        Map<String, String> mp
                = Stream
                .of(new String[][]{
                        {"1", "Geeks"},
                        {"2", "For"},
                        {"3", "Geeks"}})
                .collect(Collectors.collectingAndThen(Collectors.toMap(p -> p[0], p -> p[1]),
                        Collections::<String, String>
                                unmodifiableMap));
        System.out.println(mp);
    }
}
